package cn.xdj.blue.java.netty5.simple.server;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class HelloServerHandler extends ChannelHandlerAdapter {
	@Override
	//@Skip
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		System.out.println(ctx.channel().localAddress().toString()+"--channelActive--");
	}

	@Override
	//@Skip
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		System.out.println("----channelRead----");
		ByteBuf buf = (ByteBuf) msg;
		byte[] request = new byte[buf.readableBytes()];
		buf.readBytes(request);
		String body = new String(request,"UTF-8");
		System.out.println("----c_request:"+body+"----");
		ByteBuf response = Unpooled.copiedBuffer("coming from server!".getBytes());
		ctx.write(response);
		
	}
	@Override
	//@Skip
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		ctx.flush();
	}
	@Override
	//@Skip
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
			throws Exception {
		ctx.close();
		System.out.println("异常信息：\r\n"+cause.getMessage());
	}
	@Override
	//@Skip
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		System.out.println(ctx.channel().localAddress().toString()+"--channelInactive--");
	}
	
	
}
